package com.leyou.goods.service;


import com.leyou.goods.client.BrandClient;
import com.leyou.goods.client.CategoryClient;
import com.leyou.goods.client.GoodsClient;
import com.leyou.goods.client.SpecClient;
import com.leyou.item.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * *
 * Created by IntelliJ IDEA.
 * Author: caoLei
 * Date: 2018/11/1
 * Time: 9:38
 * *
 */
@Service
public class GoodsService {

    @Autowired
    private BrandClient brandClient;
    @Autowired
    private CategoryClient categoryClient;
    @Autowired
    private GoodsClient goodsClient;
    @Autowired
    private SpecClient specClient;


    public Map<String, Object> loadModel(Long spuId) {


        //查询spu
        Spu spu = this.goodsClient.querySpuBySpuId(spuId);
        //查询spuDetail
        SpuDetail spuDetail = this.goodsClient.querySpuDetailById(spuId);
        //查询spu下的所有sku
        List<Sku> skus = this.goodsClient.querySkusBySpuId(spuId);
        //查询brand
        Brand brand = this.brandClient.queryBrandById(spu.getBrandId());
        //查询分类
        //构建ids集合
        List<Long> ids = Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3());
        List<String> names = this.categoryClient.queryNameByIds(ids);
        //初始化一个集合
        List<Map<String, Object>> categories = new ArrayList<>();
        for (int i = 0; i < ids.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", ids.get(i));
            map.put("name", names.get(i));//一个id对应一个name
            categories.add(map);
        }
        //查询组内参数
        List<SpecGroup> specGroups = this.specClient.queryGroupWithPramByCid(spu.getCid3());
        //查询所有特有规格参数
        List<SpecParam> params = this.specClient.querySpecParams(null, spu.getCid3(), false, false);
        Map<Long, String> paramMap = new HashMap<>();
        params.forEach(param->{
            paramMap.put(param.getId(), param.getName());

        });
        //我们把需要的数据封装为一个map集合，方便获取数据（也可以封装为一个pojo，但是不用）
        Map<String, Object> modelMap = new HashMap<>();
        modelMap.put("spu", spu);
        modelMap.put("spuDetail", spuDetail);
        modelMap.put("skus", skus);
        modelMap.put("brand", brand);
        modelMap.put("categories", categories);
        modelMap.put("groups", specGroups);
        //获取特殊的规格参数  paramid,paramname
        modelMap.put("paramMap", params);
        return modelMap;

    }

}

